/*
 * @文件描述:电机控制类,用于控制电机,不需要手动实例化,已经挂在gameobject上,获取组件即可
 * @版本:
 * @作者:周晨阳
 * @Date:2021-09-26 19:54:18
 */
using System;
using myConection;
using UnityEngine;
using static COM_CODE;

namespace Motor_Control
{
    //枚举定义
    /// <summary>
    /// 手臂移动的枚举类型
    /// </summary>
    public enum HAND_DIRECTION
    {
        UP = 0,
        DOWN = 1,
        LEFT = 2,
        RIGHT = 3,
    }
    /// <summary>
    /// 电机id
    /// </summary>
    public enum MOTOR_ID
    {
        MOTOR_ID_1 = 1,
        MOTOR_ID_2 = 2,
    }
    /// <summary>
    /// 电机旋转方向
    /// </summary>
    public enum MOTOR_DIRECTION
    {
        LEFT = 0,
        RIGHT = 1,
    }

    public class motorControl : MonoBehaviour
    {
        private Analysis analysis;

        void Awake()
        {

        }
        void Start()
        {
            GameObject conObj = GameObject.Find("GameObject");
            analysis = (Analysis)conObj.GetComponent(typeof(Analysis));
        }

        /// <summary>
        /// 单次获取电机的位置
        /// </summary>
        /// <param name="motorID"> 电机ID</param>
        /// <param name="callback">回调函数，数据更新后会自动调用此回调函数</param>
        public void GetMotorPos(MOTOR_ID motorID, Action<object> callback)
        {
            RequestCommand<int> command = new RequestCommand<int>(
                                                            REQUEST_CODE.REQUEST_DATA,
                                                            TYPE_CODE.MOTOR_POS,
                                                            (int)motorID,
                                                           callback);
            command.execute();
        }


        /// <summary>
        /// 连续获取电机位置
        /// </summary>
        /// <param name="motorID"> 电机ID</param>
        /// <param name="hz">数据发送的频率，单位hz </param>
        /// <param name="callback">回调函数，数据更新后会自动重复调用此回调函数 </param>
        public void StartContinuousMotorPos(MOTOR_ID motorID, float hz, Action<object> callback)
        {
            RequestCommand<MOTOR_ID, float> command = new RequestCommand<MOTOR_ID, float>(
                                                            REQUEST_CODE.REQUEST_DATA,
                                                            TYPE_CODE.MOTOR_POS,
                                                            motorID,
                                                            hz,
                                                            callback);
            command.execute();
        }
        /// <summary>
        /// 停止获取电机的位置
        /// </summary>
        /// <param name="motorID"> 电机ID</param>
        public void StopContinuousMotorPos(MOTOR_ID motorID)
        {

        }

        /// <summary>
        /// 控制电机相对转动
        /// </summary>
        /// <param name="motorID"> 电机ID</param>
        /// <param name="direction">电机转动的方向</param>
        /// <param name="distance">电机转动的距离</param>
        public void MotorControlRel(MOTOR_ID motorID, MOTOR_DIRECTION direction, int distance)
        {

        }
        /// <summary>
        /// 控制电机绝对转动
        /// </summary>
        /// <param name="motorID"> 电机ID</param>
        /// <param name="direction">电机转动的方向</param>
        /// <param name="distance">电机转动的距离</param>
        public void MotorControlAbs(MOTOR_ID motorID, MOTOR_DIRECTION direction, int distance)
        {

        }
        /// <summary>
        /// 立即停止电机转动
        /// </summary>
        /// <param name="motorID">电机ID</param>
        public void StopMotor(MOTOR_ID motorID)
        {
            RequestCommand<MOTOR_ID> command = new RequestCommand<MOTOR_ID>(
                                                            REQUEST_CODE.COMMAND,
                                                            TYPE_CODE.STOP_MOTOR,
                                                            motorID);
            command.execute();
        }
        /// <summary>
        ///  控制手臂移动方向,是MotorControl的高阶版,直接输入上下左右的代码即可
        /// </summary>
        /// <param name="direction"></param>
        public void HandControl(HAND_DIRECTION direction, int distance)
        {
            RequestCommand<int, int> command = new RequestCommand<int, int>(
                                                            REQUEST_CODE.COMMAND,
                                                            TYPE_CODE.HAND_CONTROL,
                                                            (int)direction, distance);
            command.execute();
        }
    }
}